/**
 * Created by IntelliJ IDEA.
 * User: jiyufei
 * Date: 2018/5/9
 * Time: 14:14
 **/
package com.jiyufei.redis;

import redis.clients.jedis.Jedis;

import java.util.Map;

public class RedisCenter {
    private Jedis jedis;
    private long res;
    private String res1;
    private boolean res2;
    
    public RedisCenter(){
       
        jedis = new Jedis("127.0.0.1",6379);
        
    }

    public boolean isExists(String key,String field){
        try {

            res2 = jedis.hexists(key,field);
        }catch (Exception e){
            //logCenter.sendLog("HlasYunYing","云英","[RedisCenter.setData]","高","写入redis失败,key:" + key + " field:" + field,e.getMessage());
            System.out.println("setData error:\r\n" + e.getMessage());
            res2 = false;
        }
        return res2;
    }

    public long setData(String key,String field,String value){
        try {
            
            res = jedis.hset(key,field,value);
        }catch (Exception e){
            //logCenter.sendLog("HlasYunYing","云英","[RedisCenter.setData]","高","写入redis失败,key:" + key + " field:" + field,e.getMessage());
            System.out.println("setData error:\r\n" + e.getMessage());
            res = -100;
        }
        return res;
    }
    
    public String setMData(String key, Map<String,String> data){
        return jedis.hmset(key,data);
    }
    
    public String getData(String key,String field){
        try {
            res1 = jedis.hget(key,field);
            /*
            if(jedis.hexists(key,field)){
                res = jedis.hdel(key,field);
            }
            */
        }catch (Exception e){
            //logCenter.sendLog("HlasYunYing","云英","[RedisCenter.getData]","高","读取redis失败,key:" + key + " field:" + field,e.getMessage());
            System.out.println("getData error:\r\n" + e.getMessage());
            return null;
        }
        return res1;
    }
    
    public Map<String,String> getAll(String key){
        return jedis.hgetAll(key);
    }
    
    public Long delData(String key,String field){
        if(jedis.hexists(key,field)){
            res = jedis.hdel(key,field);
        }else{
            res = -1;
        }
        return res;
    }
    
}
